package utils;

/**
 * Created by aceyin on 2015/5/31 0031.
 */
public final class Ids {

    /**
     * IMPORTANT: 不要修改这个数组的值，否则会影响数据库分库的hash算法。
     */
    private static final int[] USER_ID_BIT_OFFSET = new int[]{
            24, 78, 1, 267, 154, 36, 86, 1796, 34, 346,
            72, 874, 95, 2, 10, 8, 3, 47, 56, 5,
            62, 50, 156, 7, 5, 89, 64, 46, 35, 2745,
            26, 37, 812, 79, 503, 376, 901, 7, 403, 204,
            12, 30, 27, 460, 157, 480, 103, 704, 747, 885,
            456, 207, 889, 38, 94, 229, 578, 780, 24, 494,
            193, 734, 559, 714, 937, 255, 199, 751, 156, 840,
            30, 5, 30, 64, 27, 12, 98, 77, 91, 49,
            726, 751, 56, 385, 177, 90, 216, 682, 46, 675,
            33, 22, 112, 120, 87, 59, 189, 161, 41, 154
    };

    /**
     * Generate user ID by username.
     *
     * @param username
     * @return
     */
    public static long get(String username) {
        if (username == null || username.trim().length() <= 0) {
            return 0;
        }
        long id = 0l;
        int len = username.length();
        int offsetSize = USER_ID_BIT_OFFSET.length;
        for (int i = 0; i < len; i++) {
            int offset = USER_ID_BIT_OFFSET[i % offsetSize];
            id += username.charAt(i) * offset;
        }
        return id;
    }
}
